package com.yyk.yykywd

import com.yyk.yykywd.util.TimeUtil.YYYY_MM_DD
import com.yyk.yykywd.util.TimeUtil.YYYY_MM_DD_HH_MM
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*

/**
 * 文件描述：
 * Created by guo on 2019-11-28.
 */
fun compareDate(DATE1: String, DATE2: String, format: String): Int {
    val dt1: Long = getTimeViaFormat(DATE1, format)
    val dt2: Long = getTimeViaFormat(DATE2, format)
    return if (dt1 > dt2) {
        1
    } else if (dt1 < dt2) {
        -1
    } else {
        0
    }
    //前者大于后者 返回大于0 反之小于0
}

/**
 * 获取以秒为单位的时间戳
 */
fun getTimeViaFormat(DATE1: String, format: String): Long {
    val df = SimpleDateFormat(format)
    try {
        val dt1 = df.parse(DATE1)
        return dt1.time / 1000
    } catch (e: ParseException) {
        e.printStackTrace()
    }
    return 0
}

fun getCommonTime(time: Int): String {
    return getDayWithPattern(time.toLong() * 1000, YYYY_MM_DD_HH_MM)
}

fun getCommonTimeWithYMD(time: Int): String {
    return getDayWithPattern(time.toLong() * 1000, YYYY_MM_DD)
}

fun getDayWithPattern(timeStamp: Long, format: String): String {
    val sdf = SimpleDateFormat(format)
    return sdf.format(Date(timeStamp))
}

fun transToTimeStamp(date: String, format: String): Long {
    return SimpleDateFormat(format).parse(date).time / 1000
}